<?php
/**
 *   Funkcje pliku:
 *   School - train stats
 *
 *   @name                 : train.php                            
 *   @copyright            : (C) 2004,2005,2006,2007 Vallheru Team based on Gamers-Fusion ver 2.5
 *   @author               : thindil <thindil@users.sourceforge.net>
 *   @version              : 1.3
 *   @since                : 04.03.2007
 *
 */

//
//
//       This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation; either version 2 of the License, or
//   (at your option) any later version.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program; if not, write to the Free Software
//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// $Id: train.php 931 2007-03-04 12:08:56Z thindil $

$title = "Szkolenie";
require_once("includes/head.php"); if (!$_GET['ajax']) require_once("includes/head_start.php"); 


 // if($player -> id !='2' && $player -> id != '126')
 // {
  // error("Remont");
 
 // }
/**
* Get the localization for game
*/
require_once("languages/".$player -> lang."/train.php");

if ($player -> location != 'Altara' && $player -> location != 'Ardulith' && $player -> location != 'Miastotrzy') 
{
    error (ERROR);
}

if ($player -> hp == 0) 
{
    error (YOU_DEAD." <a href=\"city.php\">".BACK."</a>.");
}

if (!$player -> race)
{
    error(NO_RACE." <a href=\"city.php\">".BACK."</a>.");
}

if (!$player -> clas) 
{
    error(NO_CLASS." <a href=\"city.php\">".BACK."</a>.");
}

$fltStat = (log10($player -> level) + 1);

$intStrcost = ceil($player -> strength / $fltStat);
$intAgicost = ceil($player -> agility / $fltStat);
$intConcost = ceil($player -> cond / $fltStat);
$intSpecost = ceil($player -> speed / $fltStat);

if ($player -> location == 'Ardulith')
{
    $intIntcost = ceil($player -> inteli / $fltStat);
    $intWiscost = ceil($player -> wisdom / $fltStat);
}
    else
{
    $intIntcost = ceil(($player -> inteli / $fltStat) - (($player -> inteli / $fltStat) / 10));
    $intWiscost = ceil(($player -> wisdom / $fltStat) - (($player -> wisdom / $fltStat) / 10));
}

$trras = $db -> GetRow("SELECT `agility`, `strength`, `endurance`, `speed`, `wisdom`, `int` FROM `zywioly` WHERE `name`='".$player -> race."'") or die($db -> ErrorMsg());
$repeatagi =  ($trras['agility']/10);
$repeatstr =  ($trras['strength']/10);
$repeatend =  ($trras['endurance']/10);
$repeatspeed =  ($trras['speed']/10);
$repeatwis =  ($trras['wisdom']/10);
$repeatint =  ($trras['int']/10);
$smarty -> assign("Train", $repeatagi." zręczności (".$intAgicost." sztuk złota) <br> ".$repeatstr." siły (".$intStrcost." sztuk złota)<br> ".$repeatend." wytrzymałości (".$intConcost." sztuk złota) <br> ".$repeatspeed." szybkość (".$intSpecost." sztuk złota) <br> ".$repeatwis." siły woli (".$intWiscost." sztuk złota) <br> ".$repeatint." inteligencji (".$intIntcost." sztuk złota)<br>");
 

if (isset ($_GET['action']) && $_GET['action'] == 'train') 
{
    if (!isset($_POST['rep']))
    {
        error(HOW_MANY);
    }
    if (!ereg("^[1-9][0-9]*$", $_POST["rep"])) 
    {
        error (ERROR);
    }
    $arrStats = array('strength', 'agility', 'inteli', 'szyb', 'wytrz', 'wisdom');
    if (!in_array($_POST['train'], $arrStats)) 
    {
        error (ERROR);
    }

$trras = $db -> GetRow("SELECT `agility`, `strength`, `endurance`, `speed`, `wisdom`, `int` FROM `zywioly` WHERE `name`='".$player -> race."'") or die($db -> ErrorMsg());
switch($_POST['train'])
{
 case "strength":
 $repeat = ($trras['strength']/10);
 break;

 case "agility":
 $repeat = ($trras['agility']/10);
 break;
 
 case "inteli":
  $repeat = ($trras['int']/10);
 break;

 case "szyb":
  $repeat = ($trras['speed']/10);
 break;
 
 case "wytrz":
  $repeat = ($trras['endurance']/10);
 break;
 
 case "wisdom":
  $repeat = ($trras['wisdom']/10);
 break;
}
   
    if ($player -> race == '') 
    {
        error (NO_RACE);
    }
    if (($player -> clas == 'Wojownik' || $player -> clas == 'Tropiciel' || $player -> clas == 'Inkwizytor' || $player -> clas == 'Druid') && $_POST["train"]) 
    {
        if ($_POST['train'] == 'inteli' || $_POST['train'] == 'wisdom') 
        {
            $repeat = ($_POST["rep"] * .4);
        }
    }
    if (($player -> clas == 'Czarodziej' || $player -> clas == 'Nekromanta') && $_POST["train"]) 
    {
        if ($_POST['train'] == 'inteli' || $_POST['train'] == 'wisdom') 
        {
            $repeat = ($_POST["rep"] * .2);
        }
    }
    if ($player -> clas == 'Rzemieślnik' && $_POST["train"]) 
    {
        if ($_POST['train'] == 'inteli' || $_POST['train'] == 'wisdom') 
        {
            $repeat = ($_POST["rep"] * .3);
        }
    }
    if ($player -> clas == 'Barbarzyńca' && $_POST["train"]) 
    {
        if ($_POST['train'] == 'inteli' || $_POST['train'] == 'wisdom') 
        {
            $repeat = ($_POST["rep"] * .3);
        }
    }
    if (($player -> clas == 'Złodziej' || $player -> clas == 'Zabójca') && $_POST["train"]) 
    {
        if ($_POST['train'] == 'inteli' || $_POST['train'] == 'wisdom') 
        {
            $repeat = ($_POST["rep"] * .3);
        }
    }
    // if ($player -> race == 'Gnom' && $_POST["train"]) 
    // {
        // if ($_POST['train'] == 'wisdom') 
        // {
            // $repeat = ($_POST["rep"] * .4);
        // }
    // }
    if ($player -> clas == '') 
    {
        error (NO_CLASS);
    }
    $gain = ($_POST["rep"] * $repeat);
	 $repeat *= $_POST['rep'];
	 
    if ($repeat > $player -> energy) 
    {
        error (NO_ENERGY);
    }
	
	//limity

	if($_POST['train'] == 'szyb')
	{
	 $cotrenuje = 'speed';
	 }
	 if($_POST['train'] == 'wytrz')
	 {
	 $cotrenuje = 'cond';
	 } 
	 if($_POST['train'] != 'szyb' && $_POST['train'] != 'wytrz')
	 {
	  $cotrenuje = $_POST['train'];
	 }
    if($player -> limiton == '0')
	{
	$limity = $expn * $player -> level + $player -> $cotrenuje;
	$db -> Execute("UPDATE `players` SET `limit`=".$limity.", `limiton`=1 WHERE id=".$player -> id) or die($db -> ErrorMsg());
    }
	
	if($player -> limit > ($expn * $player -> level * $player -> $cotrenuje)) 
	{
	  error("Nie możesz tyle ćwiczyć, musisz awansować na wyższy poziom!");
	}
	if($gain > $player -> limit)
	{
      error("Nie możesz tyle ćwiczyć, musisz awansować na wyższy poziom!");
	}
	
	$smarty -> assign("Limit", $player -> limit);
    if ($_POST['train'] == 'strength') 
    {
        $cecha = T_STR;
        $intCost = $intStrcost;
    }
    if ($_POST['train'] == 'agility') 
    {
        $cecha = T_AGI;
        $intCost = $intAgicost;
    }
    if ($_POST['train'] == 'inteli') 
    {
        $cecha = T_INT;
        $intCost = $intIntcost;
    }
    if ($_POST['train'] == 'szyb') 
    {
        $cecha = T_SPEED;
        $intCost = $intSpecost;
    }
    if ($_POST['train'] == 'wytrz') 
    {
        $cecha = T_CON;
        $intCost = $intConcost;
    }
    if ($_POST['train'] == 'wisdom') 
    {
        $cecha = T_WIS;
        $intCost = $intWiscost;
    }
    $intCost = ceil($intCost + (($_POST['rep'] - 1) * 0.03));
    $intCost2 = ($intCost * $_POST['rep']);
    if ($player -> credits < $intCost2) 
    {
        error (NO_MONEY.$intCost2.GOLD_COINS);
    }
    $smarty -> assign(array("Allcost" => ALL_COST,
                            "Maxcost" => $intCost2,
                            "Train" => $_POST['train'],
                            "Rep" => $_POST['rep'],
                            "Goldcoins" => GOLD_COINS));
    if (isset($_GET['step']) && $_GET['step'] == 'next')
    {
	 	// if($player -> limit > ($expn * $player -> level * $player -> $cotrenuje)) 
	// {
	  // error("Nie możesz tyle ćwiczyć, musisz awansować na wyższy poziom!");
	// }
	if($gain > $player -> limit)
	{
      error("Nie możesz tyle ćwiczyć, musisz awansować na wyższy poziom!");
	}
        if ($_POST['train'] == 'wytrz') 
        {
            $intCondition = floor($player -> cond + $gain);
            if ($intCondition > $player -> cond)
            {
                $intGain = $intCondition - floor($player -> cond);
                $db -> Execute("UPDATE `players` SET `max_hp`=`max_hp`+".$intGain." WHERE `id`=".$player -> id);
            } 
        }

		        $db -> Execute("UPDATE `players` SET `energy`=`energy`-".$repeat.", ".$_POST['train']."=".$_POST['train']."+".$gain.", `limit`=`limit`-".$gain.", `credits`=`credits`-".$intCost2." WHERE `id`=".$player -> id) or die($db -> ErrorMsg());
        error (YOU_GAIN.$gain." ".$cecha.YOU_PAY.$intCost2.GOLD_COINS);
    }
}

/**
* Initialization ov variables
*/
if (!isset($_GET['action']))
{
    $_GET['action'] = '';
}
if (!isset($_GET['step']))
{
    $_GET['step'] = '';
}

/**
* Assign variables to template and display page
*/
$smarty -> assign(array("Traininfo" => TRAIN_INFO,
                        "Trstr" => TR_STR,
                        "Tragi" => TR_AGI,
                        "Trspeed" => TR_SPEED,
                        "Trcon" => TR_CON,
                        "Trint" => TR_INT,
                        "Trwis" => TR_WIS,
                        "Iwant" => I_WANT,
                        "Tamount" => T_AMOUNT,
                        "Atrain" => A_TRAIN,
                        "Action" => $_GET['action'],
                        "Step" => $_GET['step']));
$smarty -> display ('train.tpl');

if (!$_GET['ajax']) require_once("includes/foot.php");
?>
